package com.itheima.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.exception.BusinessException;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetMealMapper;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired(required = false)
    private CategoryMapper categoryMapper;

    /**
     * 新增类别
     *
     * @param category
     */
    @Override
    public void save(Category category) {

        //1.判晰类别名称是否重复
        //根据类别名称查找数据库中的类别对象
        Category dbCategory = categoryMapper.findByName(category.getName());
        //判断对象如果不为null，说明己存在，抛出业务异常，提示该类别已存在，请修改类别名称
        if (dbCategory!=null){
            throw new BusinessException("该类别已存在，请修改类别名称呀");
        }
        //2.完善category的数据
        LocalDateTime now = LocalDateTime.now();
        //创建时间
        category.setCreateTime(now);
        //修改时间
        category.setUpdateTime(now);
        //3.新增操作
        categoryMapper.save(category);
    }

    /**
     * 分页查询类别信息
     *
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public Page<Category> findByPage(Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<Category> categoryList = categoryMapper.findByPage();

        PageInfo<Category> pageInfo = new PageInfo<>(categoryList);

        Page<Category>pageObj =new Page<>();
        pageObj.setRecords(pageInfo.getList());
        pageObj.setTotal(pageInfo.getTotal());
        return pageObj;
    }
    @Autowired(required = false)
    private DishMapper dishMapper;
    @Autowired(required = false)
    private SetMealMapper setMealMapper;
    /**
     * 根据id删除
     *
     * @param id
     */
    @Override
    public void delete(long id) {
        //获取类别菜品数量
        Integer dishCount = dishMapper.findByCid(id);

        //判断是否大于1,则抛出"该类别有关联别的菜品无法删除"
        if (dishCount>=1){
            throw  new BusinessException("该类别有关联别的菜品无法删除");
        }
        //获取套餐信息数量
        Integer setMealCount = setMealMapper.findByCid(id);
        //判断是否大于1,则抛出"该类别有关联套餐信息无法删除"
        if (setMealCount>=1){
            throw  new BusinessException("该类别有关联套餐信息无法删除");
        }
        //无关联才删除
        categoryMapper.delete(id);
    }

    /**
     * 修改类别
     *
     * @param category
     */
    @Override
    public void update(Category category) {
        ///1.判断修改的类别名称是否已存在
        // 查询这个类别名称的类别数量
        Integer count = categoryMapper.selectByNameCount(category);
        //判断数量>0,说明这个类别名称已被占用
        if (count>0){
            throw  new BusinessException("该类别已存在,修改失败");
        }
        //2.完善信息（修改时间)
        category.setUpdateTime(LocalDateTime.now());
        //3;.调用数据访问层修改
        categoryMapper.update(category);
    }

    /**
     * 根据type查找类别列表数据
     *
     * @param type
     * @return
     */
    @Override
    public List<Category> findAllByType(Integer type) {
        return categoryMapper.findAllByType(type);
    }


}
